package xunmeng.dao.impl;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import xunmeng.dao.UserDao;
import xunmeng.domain.Admin;
import xunmeng.domain.User;
import xunmeng.util.JDBCUitls;

import java.util.List;

public class UserDaoImpl implements UserDao {
    //创建JDBCTemplate对象操做数据库
    JdbcTemplate jdbcTemplate=new JdbcTemplate(JDBCUitls.getDataSource());
    @Override
    public List<User> findAll() {
        //调用数据库获取数据
        //定义SQL语句
        String sql="select * from user";
        List<User> users=jdbcTemplate.query(sql,new BeanPropertyRowMapper<User>(User.class));
        return users;
    }

    @Override
    public Admin checkUser(Admin admin) {
        String sql="select adminId,username as name ,password from admin where username=? and password=?";
        List<Admin> admins=jdbcTemplate.query(sql,new BeanPropertyRowMapper<Admin>(Admin.class),admin.getName(),admin.getPassword());
        if(admins.size()==0){
            return null;
        }
        return admins.get(0);
    }

    @Override
    public boolean addUser(User user) {
        String sql="insert into user(name,gender,age,address,qq,email) values(?,?,?,?,?,?)";
        int rs=jdbcTemplate.update(sql,user.getName(),user.getGender(),user.getAge(),user.getAddress(),user.getQq(),user.getEmail());
        return rs!=0;
    }

    @Override
    public void delUser(Integer id) {
        String sql="delete from user where id= ? ";
        jdbcTemplate.update(sql,id);
    }
}
